﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.Administration;

namespace Office365.Helper.BCSModeler
{
    public class BCSModeler
    {
        #region Privates
        private string _siteUrl;
        private string _path;
        private BcsTypes _type;
        private AuthenticationModes _authenticationMode = AuthenticationModes.UsersIdentity;
        private ImpersonationLevels _impersonationLevel = ImpersonationLevels.None;
        private MetadataConnectionModes _metadataConnectionMode = MetadataConnectionModes.Wsdl;
        private MetadataAuthenticationModes _metadataAuthenticationMode = MetadataAuthenticationModes.UsersIdentity;
        #endregion

        #region Public Properties
        public AuthenticationModes AuthenticationMode
        {
            get { return _authenticationMode; }
            set { _authenticationMode = value; }
        }

        public ImpersonationLevels ImpersonationLevel
        {
            get { return _impersonationLevel; }
            set { _impersonationLevel = value; }
        }

        public MetadataConnectionModes MetadataConnectionMode
        {
            get { return _metadataConnectionMode; }
            set { _metadataConnectionMode = value; }
        }

        public MetadataAuthenticationModes MetadataAuthenticationMode
        {
            get { return _metadataAuthenticationMode; }
            set { _metadataAuthenticationMode = value; }
        }
        #endregion

        #region Public Enumerations
        public enum BcsTypes
        {
            Bdcm,
            Database,
            Wcf
        }

        public enum AuthenticationModes
        {
            UsersIdentity,
            BdcIdentity,
            ImpersonateWindowsIdentity,
            ImpersonateCustomIdentity
        }

        public enum ImpersonationLevels
        {
            None,
            Anonymous,
            Identification,
            Impersonation,
            Delegation
        }

        public enum MetadataConnectionModes
        {
            Wsdl,
            MetadataExchange
        }

        public enum MetadataAuthenticationModes
        {
            UsersIdentity,
            BdcIdentity,
            ImpersonateWindowsIdentity,
            ImpersonateCustomIdentity
        }
        #endregion

        #region cstors
        /// <summary>
        /// Access modifier set to 'private' to force the use of the constructor with parameters.
        /// </summary>
        private BCSModeler()
        {
        }

        /// <summary>
        /// Creates a new instance of BCSModeler.
        /// </summary>
        /// <param name="siteUrl">The URL of the SharePoint site.</param>
        public BCSModeler(string siteUrl)
        {
            if (!string.IsNullOrEmpty(siteUrl))
                _siteUrl = siteUrl;
            else
                throw new ArgumentNullException("siteUrl");

        }
        #endregion

        /// <summary>
        /// Installs a new BCS model into the SharePoint site. 
        /// </summary>
        /// <param name="type">The type of connection.</param>
        /// <param name="path">Either a database connection string or the URL of the web service.</param>
        public void Generate(BcsTypes type, string path)
        {
            if (!string.IsNullOrEmpty(path))
                _path = path;
            else
                throw new ArgumentNullException("path");

            switch (type)
            {
                case BcsTypes.Bdcm:
                    Bdcm bdcm = new Bdcm(_siteUrl, _path);
                    bdcm.Import();
                    break;
                case BcsTypes.Wcf:
                    throw new NotImplementedException();
                    Wcf service = new Wcf(_siteUrl, _path);
                    service.Generate();
                    break;
                case BcsTypes.Database:
                    // TODO: Implementation for BcsType.Database
                    throw new NotImplementedException();
                    break;
            }
        }




    }
}
